
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from numpy import nanstd
from wcferry.wxmsg import WxMsg
from robot import Robot
import xinghuoceshi
from queue import Empty
from wcferry import Wcf
import signal
from threading import Thread
from random import *
from enum import IntEnum, unique
flag= True
import xml.etree.ElementTree as ET

__version__ = "39.0.10.1"



wcf = Wcf(debug=True)

# huo_qu=wcf.get_chatroom_members("50298454647@chatroom")

contacts=wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
print(contacts)
huo_qu=wcf.get_chatroom_members("50298454647@chatroom")

myTypes=wcf.get_msg_types()
print(myTypes)


def handler(sig,frame):
    print("自杀")
    wcf.cleanup()


signal.signal(signal.SIGINT,handler)











#wcf.send_text ("w","50298454647@chatroom")18362266822@chatroom wxid_p4sug28onvvs22 wxid_iw4lcu9gybgw22
# contacts=wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")#获取信息
# print(contacts)
def xian_chat(msg:WxMsg):
    #if msg.roomid("18362266822@chatroom,{wxid_p4sug28onvvs22}") or msg._is_self("wxid_p4sug28onvvs22,wxid_iw4lcu9gybgw22"):

        if msg.roomid :
            if msg.is_at(wcf.self_wxid):
                q=msg.content.replace("@python炒面","")
                huo_qu=wcf.get_chatroom_members(msg.roomid)
                wcf.send_text (f"@{huo_qu[msg.sender]},你好，我来了",msg.roomid,msg.sender)
                xinghuoceshi.wmyy(q,msg.roomid ,wcf,msg.sender)
            if msg.is_at(wcf.self_wxid):
                wcf.send_pat_msg(msg.roomid,msg.sender)
        else:
            

            if not msg._is_self:
                reply=msg.sender
                q=msg.content
                wcf.send_text("你好，我来了",reply,"")
                xinghuoceshi.wmyy(q,reply,wcf)
                wcf.send_pat_msg(msg.roomid,msg.sender)
                
        
                
            

def dongzuo(msg:WxMsg):
    tupian=int(random()*13+1)
    if msg.roomid:
        reple=msg.roomid
    else:
        reply=msg.sender
    wcf.send_image("C:\\Users\\wang\\Pictures\\tu\\"+str(tupian)+".jpg",reply)

    pass

def paipai(msg:WxMsg):
    pass
        
   



def dauo_tu(msg:WxMsg):
    tupian=int(random()*11+1)
    if msg.roomid:
        reple=msg.roomid
    else:
        reply=msg.sender
    wcf.send_image("C:\\Users\\wang\\Pictures\\tu\\"+str(tupian)+".jpg",reply)


    pass
def hao_yu(msg:WxMsg):
    try:
        xml = ET.fromstring(msg.content)
        v3 = xml.attrib["encryptusername"]
        v4 = xml.attrib["ticket"]
        scene = int(xml.attrib["scene"])
        wcf.accept_new_friend(v3,v4,scene)

    except Exception as e:
        print(e)
        
    pass
def chuil_new_member(msg:WxMsg):
    global huo_qu
    huo_qu=wcf.get_chatroom_members(msg.roomid)
    yaoqing=msg.content
    start= 0
    if "邀请" in yaoqing:
        start=yaoqing.index("邀请")+2
        end=yaoqing.index("加入了群聊")
    if "通过扫描" in yaoqing:
        end=yaoqing.index("通过扫描")
    wcf.send_text(f"欢迎{yaoqing[start:end]}加入机器人大乱斗！！！！！！！！！！",msg.roomid,"")






def wmyy(msg:WxMsg):
    if msg.type ==1:
        xian_chat(msg)

    if msg.type==3:
        dauo_tu(msg)

    if msg.type==37:
        hao_yu(msg)
    
    if msg.type==47:
        dongzuo(msg)
        
    if msg.type==922746929:
        paipai(msg)
    if msg.roomid=="50298454647@chatroom":
        if msg.type==10000:
            if "加入" in msg.content:
                chuil_new_member(msg)
    
def enablerec():
    wcf.enable_receiving_msg()
    while flag:
        try:
            msg = wcf.get_msg()
            wmyy(msg)
            
            # if msg.roomid == "50298454647@chatroom":
            #     if msg.is_at(wcf.self_wxid):
            #         q=msg.content.replace("@python炒面","")
            #         #wcf.send_text ("你好","50298454647@chatroom")
            #         xinghuoceshi.main(
            #             appid="7eab712e",
            #             api_secret="YzEyMjA3MjUxYTZmYWZmNmM2Y2RhMzg4",
            #             api_key="b6bfb48cd24d9f35223779885892d044",
            #             #appid、api_secret、api_key三个服务认证信息请前往开放平台控制台查看（https://console.xfyun.cn/services/bm35）
            #             gpt_url="wss://spark-api.xf-yun.com/v3.5/chat",
            #             # Spark_url = "ws://spark-api.xf-yun.com/v3.1/chat"  # v3.0环境的地址
            #             # Spark_url = "ws://spark-api.xf-yun.com/v2.1/chat"  # v2.0环境的地址
            #             # Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat"  # v1.5环境的地址
            #             domain="generalv3.5",
            #             # domain = "generalv3"    # v3.0版本
            #             # domain = "generalv2"    # v2.0版本
            #             # domain = "general"    # v2.0版本
            #             query=q,
            #             wcf=wcf
            #         )
                    # wcf.send_text("[得意]","18362266822@chatroom","")
        except Empty:
            continue
        except Exception as e:
            print(e)
Thread(target=enablerec,name="循环收消息",daemon=True).start()
while True:
        input("按回车开启或关闭接受消息")
        flag = not flag
        if flag:
            print("正在接受消息")
        else:
            print("关闭了接受消息")



